export function arrayRemove(array, item) {
  let newArray = [];
  let isChanged = false;
  for (let oldItem of array) {
    if (oldItem !== item) {
      newArray.push(oldItem);
    } else {
      isChanged = true;
    }
  }
  if (isChanged){
    return newArray;
  } else {
    return array;
  }
}

export function arrayRemoveByObjKey(array, key, keyValue){
  let newArray = [];
  let isChanged = false;
  for (let item of array) {
    if (item[key] !== keyValue) {
      newArray.push(item);
    } else {
      isChanged = true;
    }
  }
  if (isChanged){
    return newArray;
  } else {
    return array;
  }
}

export function objRemove(obj, id) {
  let newObj = {}
  let isChanged = false;
  for (let oldId in obj) {
    if (oldId!=id){
      newObj[oldId] = obj[oldId];
    } else {
      isChanged = true;
    }
  }
  if (isChanged){
    return newObj;
  } else {
    return obj;
  }
}

export function objRemoveMany(obj, names){
  let newObj= {};
  let isChanged = false;
  for (let oldName in obj){
    if (!names.includes(oldName)){
      newObj[oldName] = obj[oldName];
    } else {
      isChanged = true;
    }
  }
  if (isChanged){
    return newObj;
  } else {
    return obj;
  }
}

export function startsWith(str1, str2) {
  if (typeof(str1) !== 'string'){
    return false;
  }
  return str1.substr(0,str2.length) === str2;
}

export function countBase(array, matchFun){
  let total = 0;
  for (let item of array){
    if (matchFun(item)){
      total++;
    }
  }
  return total
}

export function countObjByKey(array, key, value){
  return countBase(array, (obj)=>{return obj[key]===value})
}

export function count(array, value){
  return countBase(array, (item)=>{return item===value})
}


export function isGreaterVersion(v1, v2){
    let v1s = v1.replace('v','').replace('V','').split('.');
    let v2s = v2.replace('v','').replace('V','').split('.');
    for(let i=0; i<v1s.length && i<v2s.length;i++){
        let v1_version = parseInt(v1s[i]);
        let v2_version = parseInt(v2s[i]);
        if (v1_version!==v2_version){
            return v1_version>v2_version
        }
    }
    return v1s.length > v2s.length
}